// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <melon/benchmark.h>
#include <melon/init/init.h>
#include <melon/random.h>
#include <pollux/functions/macros.h>
#include <pollux/functions/lib/benchmarks/FunctionBenchmarkBase.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>

namespace kumo::pollux::functions::test {
namespace {
class CompareBenchmark : public functions::test::FunctionBenchmarkBase {
 public:
  CompareBenchmark() : FunctionBenchmarkBase() {
    prestosql::registerComparisonFunctions();
  }

  VectorPtr makeData() {
    constexpr vector_size_t size = 1000;

    return vectorMaker_.flat_vector<int64_t>(
        size,
        [](auto row) { return row % 2 ? row : melon::Random::rand32() % size; },
        kumo::pollux::VectorMaker::null_every(5));
  }

  size_t run(const std::string& functionName) {
    melon::BenchmarkSuspender suspender;
    auto inputs = vectorMaker_.row_vector({makeData(), makeData()});

    auto exprSet = compileExpression(
        fmt::format("c0 {} c1", functionName), inputs->type());
    suspender.dismiss();

    return doRun(exprSet, inputs);
  }

  size_t doRun(exec::ExprSet& exprSet, const RowVectorPtr& row_vector) {
    int cnt = 0;
    for (auto i = 0; i < 100; i++) {
      cnt += evaluate(exprSet, row_vector)->size();
    }
    melon::doNotOptimizeAway(cnt);
    return cnt;
  }

  size_t runFastPolluxInPolluxOut() {
    melon::BenchmarkSuspender suspender;
    auto input1 = makeData();
    auto input1Flat = input1->as_flat_vector<int64_t>();
    auto input2 = makeData();
    auto input2Flat = input2->as_flat_vector<int64_t>();
    suspender.dismiss();

    int cnt = 0;
    for (auto i = 0; i < 100; i++) {
      auto result = vectorMaker_.flat_vector<bool>(1000);
      for (int j = 0; j < input1Flat->size(); j++) {
        if (input1Flat->is_null_at(j) || input2Flat->is_null_at(j)) {
          result->set_null(j, true);
        } else {
          result->set(j, input1Flat->value_at(j) == input2Flat->value_at(j));
        }
      }
      cnt += result->size();
      melon::doNotOptimizeAway(result);
    }
    melon::doNotOptimizeAway(cnt);
    return cnt;
  }

  size_t runFastPolluxInStdOut() {
    melon::BenchmarkSuspender suspender;
    auto input1 = makeData();
    auto input1Flat = input1->as_flat_vector<int64_t>();
    auto input2 = makeData();
    auto input2Flat = input2->as_flat_vector<int64_t>();
    suspender.dismiss();

    int cnt = 0;
    for (auto i = 0; i < 100; i++) {
      std::vector<std::optional<bool>> result;
      for (int j = 0; j < input1Flat->size(); j++) {
        if (input1Flat->is_null_at(j) || input2Flat->is_null_at(j)) {
          result.push_back(std::nullopt);
        } else {
          result.push_back(input1Flat->value_at(j) == input2Flat->value_at(j));
        }
      }
      cnt += result.size();
      melon::doNotOptimizeAway(result);
    }
    melon::doNotOptimizeAway(cnt);
    return cnt;
  }

  size_t runFastStdInStdOut() {
    auto buildInput = []() {
      std::vector<std::optional<int64_t>> result;
      for (auto i = 0; i < 1000; i++) {
        if (i % 5) {
          result.push_back(std::nullopt);
        } else {
          if (i % 2) {
            result.push_back(i);
          } else {
            result.push_back(melon::Random::rand32() % 1000);
          }
        }
      }
      return result;
    };

    melon::BenchmarkSuspender suspender;
    auto input1 = buildInput();
    auto input2 = buildInput();
    suspender.dismiss();

    int cnt = 0;
    for (auto i = 0; i < 100; i++) {
      std::vector<std::optional<bool>> result;
      for (int j = 0; j < input1.size(); j++) {
        if (!input1[j].has_value() || !input2[j].has_value()) {
          result.push_back(std::nullopt);
        } else {
          result.push_back(input1[j] == input2[j]);
        }
      }
      cnt += result.size();
      melon::doNotOptimizeAway(result);
    }
    melon::doNotOptimizeAway(cnt);
    return cnt;
  }
};

BENCHMARK_MULTI(Eq_Pollux) {
  CompareBenchmark benchmark;
  return benchmark.run("==");
}

BENCHMARK_MULTI(Eq_FastPolluxInPolluxOut) {
  CompareBenchmark benchmark;
  return benchmark.runFastPolluxInPolluxOut();
}

BENCHMARK_MULTI(Eq_FastPolluxInStdOut) {
  CompareBenchmark benchmark;
  return benchmark.runFastPolluxInStdOut();
}

BENCHMARK_MULTI(Eq_FastStdInStdOut) {
  CompareBenchmark benchmark;
  return benchmark.runFastStdInStdOut();
}

BENCHMARK_MULTI(Neq_Pollux) {
  CompareBenchmark benchmark;
  return benchmark.run("=");
}

BENCHMARK_MULTI(Gt_Pollux) {
  CompareBenchmark benchmark;
  return benchmark.run(">");
}
} // namespace
} // namespace kumo::pollux::functions::test

int main(int argc, char** argv) {
  melon::Init init{&argc, &argv};

  melon::runBenchmarks();
  return 0;
}
